﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElCharro01.Models;

namespace ElCharro01.Areas.Admin.Controllers
{
    //[Authorize(Roles = "Administrator")]
    public class ProductController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /Admin/Product/
        public ActionResult IntoSlideshow(bool slideshow, int ProductId)
        {
            Product Product = db.ProductSet.Single(p => p.ID == ProductId);
            Product.InSlideShow = slideshow;
            db.ObjectStateManager.ChangeObjectState(Product, EntityState.Unchanged);
            db.ProductSet.Attach(Product);
            db.ObjectStateManager.ChangeObjectState(Product, EntityState.Modified);
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        public ViewResult Index(int categoryId = 0)
        {

            ViewBag.CategoryList = db.CategorySet.ToList();

            //se non viene selezionata la visualizzazione di una categoria in particolare prendo tutti i prodotti
            if (categoryId == 0)
            {
                return View(db.ProductSet.ToList());
            }

           //se viene selezionata la visualizzazione di una sola categoria prendo tutti gli elemento di quella categoria
            else
            {
                return View(db.ProductSet.Where(p => p.Category.Id == categoryId).ToList());

            }

        }


        public ActionResult ShowSelectedProducts(int CategoryId, int ClothingTypeID)
        {

            int CatId;
            int ClothingTypeId;
            //prendo i valori dalla session
            if (CategoryId == 0)

                CatId = (int)Session["categoryId"];

            else
            {
                CatId = CategoryId;
                Session["categoryId"] = CategoryId;
            }
            if (ClothingTypeID == 0)
                ClothingTypeId = (int)Session["clothingTypeId"];
            else
            {
                ClothingTypeId = ClothingTypeID;
                Session["clothingTypeId"] = ClothingTypeID;
            }
            if (CatId != 0 && ClothingTypeId != 0)
            {
                ViewBag.CategoryName = db.CategorySet.Single(c => c.Id == CatId).Nome;
                ViewBag.ClothingTypeName = db.ClothingTypeSet.Single(c => c.ID == ClothingTypeId).Nome;
                return View(db.ProductSet.Include("ClothingType").Include("Category").Where(p => p.ClothingType.ID == ClothingTypeId && p.Category.Id == CatId).ToList());
            }
            else
                if (CatId == 0 && ClothingTypeId != 0)
                {
                    ViewBag.CategoryName = "";
                    ViewBag.ClothingTypeName = db.ClothingTypeSet.Single(c => c.ID == ClothingTypeId).Nome;
                    return View(db.ProductSet.Include("ClothingType").Include("Category").Where(p => p.ClothingType.ID == ClothingTypeId).ToList());
                }
                else
                    if (CatId != 0 && ClothingTypeId == 0)
                    {
                        ViewBag.CategoryName = db.CategorySet.Single(c => c.Id == CatId).Nome;
                        ViewBag.ClothingTypeName = "";
                        return View(db.ProductSet.Include("ClothingType").Include("Category").Where(p => p.Category.Id == CatId).ToList());
                    }
                    else
                        return RedirectToAction("Index", "Product", new { area = "Admin" });
        }

        //
        // GET: /Admin/Product/Create

        public ActionResult Create()
        {
            ViewBag.CategoryList = db.CategorySet.ToList();
            ViewBag.CloghintTypeList = db.ClothingTypeSet.ToList();
            return View();
        }

        //
        // POST: /Admin/Product/Create

        [HttpPost]
        public ActionResult Create(Product product, int CategoryId, int ClothingTypeId, bool intoSlideShow)
        {

            if (ModelState.IsValid)
            {
                //effettuo le associazioni
                product.Category = db.CategorySet.Single(p => p.Id == CategoryId);
                product.ClothingType = db.ClothingTypeSet.Single(p => p.ID == ClothingTypeId);
                product.InSlideShow = intoSlideShow;
                db.ProductSet.AddObject(product);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(product);
        }

        //
        // GET: /Admin/Product/Edit/5

        public ActionResult Edit(int id)
        {
            ViewBag.CurrentCategoryId = db.ProductSet.Single(p => p.ID == id).Category.Id;
            ViewBag.CurrentCloghintTypeId = db.ProductSet.Single(p => p.ID == id).ClothingType.ID;
            ViewBag.CategoryList = db.CategorySet.ToList();
            ViewBag.CloghintTypeList = db.ClothingTypeSet.ToList();
            Product product = db.ProductSet.Single(p => p.ID == id);
            return View(product);
        }

        //
        // POST: /Admin/Product/Edit/5

        [HttpPost]
        public ActionResult Edit(Product product, int CategoryId, int ClothingTypeId)
        {
            if (ModelState.IsValid)
            {
                db.ProductSet.Attach(product);

                Category category = db.CategorySet.Single(p => p.Id == CategoryId);
                //effettuo le associazioni

                db.ObjectStateManager.GetRelationshipManager(product);

                product.CategoryReference.Load();
                product.ClothingTypeReference.Load();

                product.ClothingTypeReference.Value = db.ClothingTypeSet.Single(p => p.ID == ClothingTypeId);
                product.CategoryReference.Value = db.CategorySet.Single(p => p.Id == CategoryId);

                db.ObjectStateManager.ChangeObjectState(product, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(product);
        }

        //
        // GET: /Admin/Product/Delete/5

        public ActionResult Delete(int id)
        {
            Product product = db.ProductSet.Single(p => p.ID == id);
            return View(product);
        }

        //
        // POST: /Admin/Product/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.ProductSet.Single(p => p.ID == id);
            db.ProductSet.DeleteObject(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}